package com.hdu.clj.dncard.databaseUtil

import android.content.ContentValues
import android.content.Context
import com.hdu.clj.dncard.database.UserDatabase
import com.hdu.clj.dncard.util.User

class UserDatabaseUtil {

    fun insert(context: Context,user:User){
        val dbHelper = UserDatabase(context,"Users.db",1)
        val db = dbHelper.writableDatabase
        val values = ContentValues().apply {
            put("acc", user.acc)
            put("pass", user.pass)
            put("exp", user.exp)
            put("isCreate", user.isCreate)
        }
        db.insert("Users", null, values)
    }

    fun isCreate(context: Context,user:String):Int{
        val dbHelper = UserDatabase(context,"Users.db",1)
        val db = dbHelper.writableDatabase
        val suser = db.rawQuery("select * from Users where acc = ?", arrayOf(user))
        if (suser.moveToFirst()){
            return suser.getInt(suser.getColumnIndex("isCreate"))
        }
        return 0
    }

    fun findexp(context: Context,user:String):Int{
        val dbHelper = UserDatabase(context,"Users.db",1)
        val db = dbHelper.writableDatabase
        val suser = db.rawQuery("select * from Users where acc = ?", arrayOf(user))
        if (suser.moveToFirst()){
            return suser.getInt(suser.getColumnIndex("exp"))
        }
        return 0
    }

    fun addexp(context: Context,user:String,add:Int){
        val dbHelper = UserDatabase(context,"Users.db",1)
        val db = dbHelper.writableDatabase
        db.execSQL("update Users set exp=exp+$add where acc = ?", arrayOf(user))
    }
}